﻿using UnityEngine;
using System.Collections;

namespace Room
{
    public class RCAIUnionActorMining : RCAIUnionActorBase
    {
        GameObject mineModel;
        protected override void OnEnter()
        {
            if(controller.owner.name == StaticData.playerData.playerId+"")
            {
                EasyTouchTool.Instance.Hide();
            }

            if (loadedMineAni)
            {
                controller.owner.PlayAnim(AniName.WA_KUANG, WrapMode.Loop);
            }
            else
            {
                controller.owner.StopCoroutine("_LoadMineAni");
                controller.owner.StartCoroutine(_LoadMineAni());
            }

            if (controller.owner.lod.Model != null)
            {

                if (mineModel != null)
                {
                    mineModel.SetActive(true);
                }
                else
                {
                    controller.owner.StopCoroutine("_LoadMineModel");
                    controller.owner.StartCoroutine(_LoadMineModel());
                }
            }
        }

        protected override void OnLeave()
        {
            if (!loadedMineAni)
            {
                controller.owner.StopCoroutine("_LoadMineAni");
            }

            controller.owner.PlayAnim(AniName.STAND);
            if (controller.owner.lod.Model != null)
            {
                if (mineModel != null)
                {
                    mineModel.SetActive(false);
                    var resName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.MiningHoe;

                    ResourceLoader.GetInstance().weaponLoader.UnloadWeapon(mineModel, resName);

                    mineModel = null;
                }
                else
                {
                    controller.owner.StopCoroutine("_LoadMineModel");
                }
            }
        }
        bool loadedMineAni = false;
        IEnumerator _LoadMineAni()
        {
            if (controller.owner.lod.Model != null)
            {

                var resourceName = controller.owner.lod.Model.name;

                yield return AssetLoad.AssetBundleManager.Instance.StartCoroutine(
                ResourceLoader.GetInstance().modelLoader.LoadModelExtend(resourceName + ConstData.extendResFieldPostfix,
                    (asset) =>
                    {
                        if (asset != null)
                        {
                            var Assets = asset as AnimClipsAsset;
                            if (Assets != null && Assets.clips.Count > 0)
                            {
                                var ac = Assets.clips[0];
                                if (ac != null)
                                {
                                    foreach (var ass in Assets.clips)
                                    {
                                        if (ass.name == AniName.WA_KUANG)
                                        {
                                            ac = ass;
                                        }
                                    }
                                    controller.owner.lod.Model.GetComponent<ModelRenderKit>().AddLifeAni(ac);
                                    controller.owner.PlayAnim(AniName.WA_KUANG);

                                    loadedMineAni = true;
                                }
                            }
                        }
                    }
                    )
                );

            }
        }


        IEnumerator _LoadMineModel()
        {
            if (controller.owner.lod.Model != null)
            {
                var tran = controller.owner.lod.Model.GetComponent<ModelRenderKit>().GetTransform("Bip001 R Hand");


                if (mineModel == null)
                {
                    var mineHoeName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.MiningHoe;

                    yield return AssetLoad.AssetBundleManager.Instance.StartCoroutine(
                       ResourceLoader.GetInstance().weaponLoader.LoadWeapon(mineHoeName, (asset) =>
                       {
                           mineModel = asset as GameObject;
                           if (mineModel != null)
                           {
                               mineModel.transform.SetParent(tran);
                               mineModel.transform.localPosition = new Vector3(-0.05f, 0.025f, -0.224f);
                               mineModel.transform.localScale = Vector3.one;
                               mineModel.transform.localRotation = Quaternion.Euler(new Vector3(-2.173f, -7.8f, -99f + 180f));

                               mineModel.SetActive(true);

                           }

                       }));
                }
            }
        }
    }
}